package com.saifan.wyy_ov.ui.onlishop;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.data.bean.CommonDataBean;
import com.saifan.wyy_ov.data.bean.GroupBuyBean;
import com.saifan.wyy_ov.data.bean.StoreBean;
import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.model.impl.GetData;
import com.saifan.wyy_ov.model.interfaces.ResponseData;
import com.saifan.wyy_ov.ui.view.CircleImageView;
import com.saifan.wyy_ov.utils.ImgUtil;

public class AdapterPromotion extends BaseAdapter {
    Context context;
    List<GroupBuyBean> groupBuyBeans;

    public AdapterPromotion(Context context, List<GroupBuyBean> groupBuyBeans) {
        this.context = context;
        this.groupBuyBeans = groupBuyBeans;
    }

    @Override
    public int getCount() {
        if (groupBuyBeans!=null && groupBuyBeans.size() > 0) {
            return groupBuyBeans.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        if (groupBuyBeans!=null && groupBuyBeans.size() > 0)
            return groupBuyBeans.get(position);
        else
            return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public List<GroupBuyBean> getGroupBuyBeans() {
        return groupBuyBeans;
    }

    public void setGroupBuyBeans(List<GroupBuyBean> groupBuyBeans) {
        this.groupBuyBeans = groupBuyBeans;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        final GroupBuyBean groupBuyBean = groupBuyBeans.get(position);
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.group_buy_view, null);
            viewHolder.ll_all_goods = (LinearLayout) convertView
                    .findViewById(R.id.ll_all_goods);
            viewHolder.ll_store = (LinearLayout) convertView
                    .findViewById(R.id.ll_store);
            viewHolder.tv_group_buy_goods_name = (TextView) convertView
                    .findViewById(R.id.tv_group_buy_goods_name);
            viewHolder.tv_postion = (TextView) convertView
                    .findViewById(R.id.tv_postion);
            viewHolder.tv_read_comments = (TextView) convertView
                    .findViewById(R.id.tv_read_comments);
            viewHolder.lv_group_buy_item = (com.saifan.wyy_ov.ui.view.NoScrollListView) convertView
                    .findViewById(R.id.lv_group_buy_item);
            viewHolder.tv_all_goods = (TextView) convertView
                    .findViewById(R.id.tv_all_goods);
            viewHolder.iv_store_img = (CircleImageView) convertView
                    .findViewById(R.id.iv_store_img);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final AdapterPromotionItem adapter = new AdapterPromotionItem(context,
                groupBuyBean, groupBuyBean.getGoodsList());
        viewHolder.tv_group_buy_goods_name.setText(groupBuyBeans.get(position)
                .getStoreName());
        ImgUtil.DisPlayImg(groupBuyBean.getHeadPhoto() + "",
                viewHolder.iv_store_img, context);
        if (groupBuyBean.getGoodsList().size() < 3) {
            viewHolder.ll_all_goods.setVisibility(View.GONE);
        } else {
            viewHolder.ll_all_goods.setVisibility(View.VISIBLE);
            viewHolder.tv_all_goods.setText("查看全部 "
                    + groupBuyBean.getGoodsList().size() + " 个商品");
        }
        viewHolder.ll_all_goods.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                adapter.setShowCount(groupBuyBean.getGoodsList().size());
                arg0.setVisibility(View.GONE);
            }
        });
        viewHolder.tv_read_comments.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(context, CommentsActivity.class);
                intent.putExtra("ID", groupBuyBean.getID());
                context.startActivity(intent);
            }
        });
        viewHolder.lv_group_buy_item.setAdapter(adapter);
        viewHolder.lv_group_buy_item
                .setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> paramAdapterView,
                                            View paramView, final int paramInt, long paramLong) {
                        // TODO Auto-generated method stub

                        // TODO Auto-generated method stub

                        CommonDataBean submitOrderRequst = new CommonDataBean();
                        submitOrderRequst.setID(groupBuyBean.getGoodsList()
                                .get(paramInt).getID());
                        submitOrderRequst.setStoreId(groupBuyBean.getID() + "");
                        GetData getDataSubmit = new GetData();
                        getDataSubmit.getDataJson(context,
                                Api.GroupBuy.GOODSDETAIL,
                                submitOrderRequst, "请稍候!", new ResponseData() {

                                    @Override
                                    public void onSuccess(String json) {
                                        // TODO Auto-generated method stub
                                        Intent intent = new Intent(context,
                                                PromotionDetailsActivity.class);
                                        Bundle bundle = new Bundle();
                                        bundle.putSerializable("goods",
                                                groupBuyBean.getGoodsList()
                                                        .get(paramInt));
                                        bundle.putSerializable("groupBuyBean",
                                                groupBuyBean);
                                        intent.putExtra("json", json);
                                        intent.putExtras(bundle);
                                        context.startActivity(intent);

                                    }

                                    @Override
                                    public void onFailed(String str) {
                                        // TODO Auto-generated method stub

                                    }
                                });

                    }
                });
        viewHolder.ll_store.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(context, StoreDetailsActivity.class);
                StoreBean storeBean = new StoreBean();
                storeBean.setID(groupBuyBean.getID());
                storeBean.setStoreName(groupBuyBean.getStoreName());
                storeBean.setHeadPhoto(groupBuyBean.getHeadPhoto());
                storeBean.setTel(groupBuyBean.getTel());
                storeBean.setBusinessDate(groupBuyBean.getBusinessDate());
                storeBean.setBusinessTime(groupBuyBean.getBusinessTime());
                storeBean.setAddress(groupBuyBean.getStoreAddress());
                storeBean.setIsDelivery(true);
                intent.putExtra("data", storeBean);
                context.startActivity(intent);
            }
        });

        return convertView;
    }

    static class ViewHolder {
        // Content View Elements
        public TextView tv_group_buy_goods_name;
        public TextView tv_postion;
        public TextView tv_read_comments;
        public com.saifan.wyy_ov.ui.view.NoScrollListView lv_group_buy_item;
        public TextView tv_all_goods;
        public LinearLayout ll_all_goods;
        CircleImageView iv_store_img;
        public LinearLayout ll_store;
        // End Of Content View Elements
    }
}
